package de.baumann.browser.model;


import android.text.TextUtils;
import android.util.Log;

import java.util.List;

import de.baumann.browser.api.net.util.OtherHttpUtil;
import de.baumann.browser.api.net.util.UserHttpUtil;
import de.baumann.browser.api.net.vo.AccountInfo;
import de.baumann.browser.api.net.vo.Game;
import de.baumann.browser.api.net.vo.InviteInfo;
import de.baumann.browser.api.net.vo.LoginInfo;
import de.baumann.browser.api.net.vo.OdinAndHashValue;
import de.baumann.browser.api.net.vo.OdinValue;
import de.baumann.browser.api.net.vo.WithdrawProgress;
import de.baumann.browser.api.net.vo.SuperiorInfo;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.api.net.vo.Version;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.ErrorMsg;
import de.baumann.browser.utils.MD5Util;
import de.baumann.browser.utils.StringUtil;
import de.baumann.browser.utils.UserDataKt;
import io.reactivex.Observable;

/**
 * @author:
 * @date:2019/1/24 10:49
 * @discription: 和用户数据相关 的 M 层
 */
public class UserModel extends BaseModel {

    public Observable<Result> registerGetSmsCode(String phone, String token) {

        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_EMPTY));
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_STYLE_ERROR));
        }

        return OtherHttpUtil.registerGetSmsCode(phone, token).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> register(String phone, String password, String smsCode, String inviteCode) {
        //手机号为空
        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_EMPTY));
        }
        //手机号格式不正确
        if (!StringUtil.isTel(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_STYLE_ERROR));
        }
        //验证码为空
        if (TextUtils.isEmpty(smsCode)) {
            return Observable.just(new Result(ErrorMsg.SMSCODE_EMPTY));
        }
        //密码为空
        if (TextUtils.isEmpty(password)) {
            return Observable.just(new Result(ErrorMsg.PASSWORD_EMPTY));
        }
        //密码格式不正确
        if (!StringUtil.checkPassword(password)) {
            return Observable.just(new Result(ErrorMsg.NOT_ENOUGH_LENGTH));
        }
        //邀请码为空
        if (TextUtils.isEmpty(inviteCode)) {
            return Observable.just(new Result(ErrorMsg.INVITECODE_EMPTY));
        }

        return UserHttpUtil.register(phone, MD5Util.md5encode(password, "UTF-8"), smsCode, inviteCode)
                .compose(RxSchedulers.ioMain());
    }

    public Observable<Result<LoginInfo>> login(String userName, String password) {
//        if (TextUtils.isEmpty(userName)) {
//            return Observable.just(new Result<>(ErrorMsg.ACCOUT_EMPTY));
//        }

//        if (TextUtils.isEmpty(password)) {
//            return Observable.just(new Result<>(ErrorMsg.PASSWORD_EMPTY));
//        }

        return UserHttpUtil.login(userName, MD5Util.md5encode(password, "utf-8")).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<String>> inviteCode(String adddress, String invite_code) {
        return UserHttpUtil.inviteCode(adddress, invite_code).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> logout() {
        LoginInfo localUser = UserDataKt.getUser();
        if (localUser == null) {
            return Observable.just(new Result("数据异常", -2));
        }
        return UserHttpUtil.logout(localUser.getUser_id()).compose(RxSchedulers.ioMain());
    }


    public Observable<Result> retrieveGetSmsCode(String phone, String token) {

        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_EMPTY));
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_STYLE_ERROR));
        }

        return OtherHttpUtil.retrieveGetSmsCode(phone, token).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> resetPassword(String phone, String password, String repassword, String smsCode) {

        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_EMPTY));
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_STYLE_ERROR));
        }

        if (TextUtils.isEmpty(smsCode)) {
            return Observable.just(new Result(ErrorMsg.SMSCODE_EMPTY));
        }

        if (TextUtils.isEmpty(password)) {
            return Observable.just(new Result(ErrorMsg.PASSWORD_EMPTY));
        }

        if (!StringUtil.checkPassword(password)) {
            return Observable.just(new Result(ErrorMsg.NOT_ENOUGH_LENGTH));
        }

        if (!password.equals(repassword)) {
            return Observable.just(new Result(ErrorMsg.PASSWORD_DIFFERENT));
        }

        return UserHttpUtil.resetPassword(phone, MD5Util.md5encode(password, "UTF-8"), smsCode)
                .compose(RxSchedulers.ioMain());
    }


    public Observable<Result> feedback(String content) {

        if (TextUtils.isEmpty(getToken())) {
            return Observable.just(new Result(ErrorMsg.NOT_LOGIN));
        }


        if (TextUtils.isEmpty(content)) {
            return Observable.just(new Result(ErrorMsg.CONTENT_EMPTY));
        }

        if (content.length() < 10) {
            return Observable.just(new Result(ErrorMsg.FEED_TIP));
        }

        String title = content;
        if (title.length() > 20) {
            title = title.substring(0, 20);
        }

        return OtherHttpUtil.feedback(title, content).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<AccountInfo>> getOdinData() {
        return UserHttpUtil.getOdinData().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Version>> updateVersion() {
        return OtherHttpUtil.updateVersion().compose(RxSchedulers.ioMain());
    }

    public Observable<Result> editHead(String headUrl) {
        return UserHttpUtil.editHead(headUrl).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> editQQ(String qq) {
        if (TextUtils.isEmpty(qq)) {
            return Observable.just(new Result(ErrorMsg.QQ_EMPTY));
        }
        return UserHttpUtil.editQQ(qq).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> editNickname(String nickname) {
        if (TextUtils.isEmpty(nickname)) {
            return Observable.just(new Result(ErrorMsg.NICKNAME_EMPTY));
        }
        return UserHttpUtil.editNickname(nickname).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> editEthUrl(String ethUrl) {
        if (TextUtils.isEmpty(ethUrl)) {
            return Observable.just(new Result(ErrorMsg.ETHURL_EMPTY));
        }
        return UserHttpUtil.editEthUrl(ethUrl).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> bindPhoneGetCode(String oldPhone, String phone, String password) {
        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_EMPTY));
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(new Result(ErrorMsg.TEL_STYLE_ERROR));
        }

        if (TextUtils.isEmpty(password)) {
            return Observable.just(new Result(ErrorMsg.PASSWORD_EMPTY));
        }

        if (!TextUtils.isEmpty(oldPhone) && oldPhone.equals(phone)) {
            return Observable.just(new Result(ErrorMsg.PHONE_EXISTED));
        }
        return UserHttpUtil.bindPhoneGetCode(phone, MD5Util.md5encode(password, "UTF-8")).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> bindPhone(String phone, String code, int type) {
        if (TextUtils.isEmpty(code)) {
            return Observable.just(new Result(ErrorMsg.SMSCODE_EMPTY));
        }
        return UserHttpUtil.bindPhone(phone, code, type).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<OdinAndHashValue>> getOdinAndHashValue() {
        return UserHttpUtil.getOdinAndHashValue().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<OdinValue>>> getOdinRecord(int startPage) {
        return UserHttpUtil.getOdinRecord(startPage).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<InviteInfo>> getSpreadInfos() {
        return UserHttpUtil.getSpreadInfos().compose(RxSchedulers.ioMain());
    }


    public Observable<Result<SuperiorInfo>> inviteAndPoolInfo() {
        return UserHttpUtil.inviteAndPoolInfo().compose(RxSchedulers.ioMain());
    }

    public Observable<Result> requestLicence(String phone, String token) {
        return OtherHttpUtil.getSmsCode(phone, 6, token).compose(RxSchedulers.ioMain());
    }

}
